<?php

/*
 *  Copyright (c) Nicholas Mossor Rathmann <nicholas.rathmann@gmail.com> 2009. All Rights Reserved.
 *
 *
 *  This file is part of OBBLM.
 *
 *  OBBLM is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OBBLM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

class Player_HTMLOUT extends Player
{
	private $data = array();

	/**
	 * playerPage function.
	 *
	 * @access public
	 * @return void
	 */
	public function playerPage()
	{
		global $lng, $coach, $data;

		$data["lang_prev"]  	= $lng->getTrn('secs/playerprofile/prev');
		$data["lang_next"]		= $lng->getTrn('secs/playerprofile/next');
		$data["lang_about"]  	= $lng->getTrn('secs/playerprofile/about');
		$data["lang_name"]  	= $lng->getTrn('secs/playerprofile/name');
		$data["lang_pos"]  		= $lng->getTrn('secs/playerprofile/pos');
		$data["lang_team"]  	= $lng->getTrn('secs/playerprofile/team');
		$data["lang_bought"] 	= $lng->getTrn('secs/playerprofile/bought');
		$data["lang_status"]	= $lng->getTrn('secs/playerprofile/status');
		$data["lang_profile"] 	= $lng->getTrn('secs/playerprofile/profile');
		$data["lang_pic"]  		= $lng->getTrn('secs/playerprofile/pic');
		$data["lang_nowrite"] 	= $lng->getTrn('secs/playerprofile/nowrite');
		$data["lang_save"]  	= $lng->getTrn('secs/playerprofile/save');

		$team = new Team($this->owned_by_team_id);

		/* Argument(s) passed to generating functions. */
		$ALLOW_EDIT = (is_object($coach) && ($team->owned_by_coach_id == $coach->coach_id || $coach->admin) && !$team->is_retired);

		/* Player pages consist of the output of these generating functions. */
		$this->_handleActions($ALLOW_EDIT); # Handles any actions/request sent.
		
		$this->_head($team);
		$this->_about($ALLOW_EDIT); //now only adds to the data array, nothing is echoed
		HTMLOUT::renderTemplate("templates/player.ms", $data);

		//Not ready yet
		//HTMLOUT::renderTemplate("templates/playerNewDesign.ms", $data);
	
		$this->_achievements();
		$this->_matchBest();
		$this->_recentGames();
	}

	/**
	 * _handleActions function.
	 *
	 * @access private
	 * @param mixed $ALLOW_EDIT
	 * @return void
	 */
	private function _handleActions($ALLOW_EDIT)
	{
		$p = $this; // Copy. Used instead of $this for readability.

		if (!$ALLOW_EDIT || !isset($_POST['type'])) {
			return false;
		}

		switch ($_POST['type'])
		{
		case 'pic':
			status(!$p->savePic('pic'));
			break;

		case 'playertext':
			if (get_magic_quotes_gpc()) {
				$_POST['playertext'] = stripslashes($_POST['playertext']);
			}
			status($p->saveText($_POST['playertext']));
			break;
		}
	}

	/**
	 * _head function.
	 *
	 * @access private
	 * @param mixed $team
	 * @return void
	 */
	private function _head($team)
	{
		global $data;
		$p = $this; // Copy. Used instead of $this for readability.

		title($p->name);
		$players = $team->getPlayers();
		$i = $next = $prev = 0;
		$end = end(array_keys($players));
		foreach ($players as $player) {
			if ($player->player_id == $p->player_id) {
				if ($i == 0) {
					$prev = $end;
					$next = 1;
				}
				elseif ($i == $end) {
					$prev = $end - 1;
					$next = 0;
				}
				else {
					$prev = $i-1;
					$next = $i+1;
				}
			}
			$i++;
		}
		if (count($players) > 1) {
			$data["prev_player_id"] = $players[$prev]->player_id;  //player_id of the prev player
			$data["next_player_id"] = $players[$next]->player_id;  //player_id of the next player
		}

	}

	/**
	 * _about function.
	 *
	 * @access private
	 * @param mixed $ALLOW_EDIT
	 * @return void
	 */
	private function _about($ALLOW_EDIT)
	{
		global $data, $lng;
		$p = $this; // Copy. Used instead of $this for readability.

		$p->skills = $p->getSkillsStr(true);
		$p->injs = $p->getInjsStr(true);

		$data["name"]				= $p->name;
		$data["nr"]     			= $p->nr;
		$data["player_id"]  		= $p->player_id;
		$data["position"]    		= $p->position;
		$data["owned_by_team_id"]  	= $p->owned_by_team_id;
		$data["team_name"]    		= $p->team_name;
		$data["date_bought"]   		= $p->date_bought;

		if ($p->is_dead) {
			$data["status"] = '<b><font color="red">DEAD</font></b> ('.$p->date_died.')';
		}
		elseif ($p->is_sold) {
			$data["status"] = '<b>SOLD</b> (' .$p->date_sold .')';
		}
		elseif ( (Player::theDoctor($p->getStatus(-1)) != "none") ){
			$data["status"] = '<b><font color="blue">$status</font></b>';
		}
		
		$data["value"] 		= $p->value/1000;
		$data["spp"] 		= $p->spp;
		$data["extra_spp"] 	= $p->extra_spp;

		if ($p->isWanted()){
			$data["isWanted"] = true;
		}

		if ($p->isInHOF()){
			$data["isInHOF"] = true;
		}
		
		$data["won"] 			= $p->won;
		$data["row_won"] 		= $p->row_won;
		$data["lost"] 			= $p->lost;
		$data["row_lost"] 		= $p->row_lost;
		$data["draw"] 			= $p->draw;
		$data["row_draw"] 		= $p->row_draw;
		$data["SG_T_PLAYER"] 	= SG_T_PLAYER;

		$data["ma"] = $p->ma;
		$data["st"] = $p->st;
		$data["ag"] = $p->ag;
		$data["av"] = $p->av;

		if (!empty($p->skills)){
			$data["skills"] = $p->skills;
		}
		
		if (!empty($p->injs)){
			$data["injs"] = $p->injs;
		}

		$data["cp"] 	= $p->cp;
		$data["intcpt"] = $p->intcpt;
		$data["td"] 	= $p->td;
		$data["bh"] 	= $p->bh;
		$data["si"] 	= $p->si;
		$data["ki"] 	= $p->ki;
		$data["cas"] 	= $p->cas;
		$data["mvp"] 	= $p->mvp;

		$txt = $p->getText();
		$data["txt"] = $p->getText();
		if ( empty($txt) ) {
			$data["getText"] = true;
		}

		if ($ALLOW_EDIT){
			$data["ALLOW_EDIT"] = true;
		}
		
		$data["pic_box"] = pic_box($p->getPic(), $ALLOW_EDIT,"" , true);
	}

	/**
	 * _achievements function.
	 *
	 * @access private
	 * @return void
	 */
	private function _achievements()
	{
		global $lng;
		$p = $this; // Copy. Used instead of $this for readability.

?>
    <div class="row">
        <div class="pboxLong">
            <div class="boxTitle3"><a href='javascript:void(0);' onClick="obj=document.getElementById('ach'); if (obj.style.display != 'none'){obj.style.display='none'}else{obj.style.display='block'};"><b>[+/-]</b></a> &nbsp;<?php echo $lng->getTrn('secs/playerprofile/ach');?></div>
            <div class="boxBody" id="ach">
                <table class="pbox">
                    <tr>
                        <td><b>Type</b></td>
                        <td><b>Tournament</b></td>
                        <td><b>Opponent</b></td>
                        <td><b>MVP</b></td>
                        <td><b>Cp</b></td>
                        <td><b>Td</b></td>
                        <td><b>Int</b></td>
                        <td><b>Cas</b></td>
                        <td><b>Score</b></td>
                        <td><b>Result</b></td>
                        <td><b>Match</b></td>
                    </tr>
                    <?php
		foreach (array('intcpt' => 'Interceptions', 'cp' => 'Completions', 'td' => 'Touchdowns', 'mvp' => 'MVP awards', 'bh+ki+si' => 'Cas') as $s => $desc) {
			$been_there = false;
			foreach ($p->getAchEntries($s) as $entry) {
				if (!$been_there)
					echo "<tr><td colspan='11'><hr></td></tr>";
?>
                            <tr>
                                <?php
				$m = $entry['match_obj'];
				if ($been_there) {
					echo '<td></td>';
				}
				else {
					echo "<td><i>$desc: " . (($desc == 'Cas') ? $p->cas : $p->$s) . "</i></td>";
					$been_there = true;
				}
?>
                                <td><?php echo get_alt_col('tours', 'tour_id', $m->f_tour_id, 'name'); ?></td>
                                <td><?php echo ($p->owned_by_team_id == $m->team1_id) ? $m->team2_name : $m->team1_name; ?></td>
                                <td><?php echo $entry['mvp']; ?></td>
                                <td><?php echo $entry['cp']; ?></td>
                                <td><?php echo $entry['td']; ?></td>
                                <td><?php echo $entry['intcpt']; ?></td>
                                <td><?php echo $entry['bh']+$entry['si']+$entry['ki']; ?></td>
                                <td><?php echo $m->team1_score .' - '. $m->team2_score; ?></td>
                                <td><?php echo matchresult_icon((($m->is_draw) ? 'D' : (($m->winner == $p->owned_by_team_id) ? 'W' : 'L'))); ?></td>
                                <td><a href='javascript:void(0)' onClick="window.open('index.php?section=fixturelist&amp;match_id=<?php echo $m->match_id;?>');">[view]</a></td>
                            </tr>
                            <?php
			}
		}
?>
                </table>
            </div>
        </div>
    </div>
    <?php

?>
    <!-- Default open/close values for box -->
    <script language="JavaScript" type="text/javascript">
        document.getElementById('ach').style.display    = 'none';
    </script>
    <?php
	}

	/**
	 * _matchBest function.
	 *
	 * @access private
	 * @return void
	 */
	private function _matchBest()
	{
		global $lng;
		$p = $this; // Copy. Used instead of $this for readability.

?>
    <div class="row">
        <div class="pboxLong">
            <div class="boxTitle3"><a href='javascript:void(0);' onClick="obj=document.getElementById('mbest'); if (obj.style.display != 'none'){obj.style.display='none'}else{obj.style.display='block'};"><b>[+/-]</b></a> &nbsp;<?php echo $lng->getTrn('secs/playerprofile/best');?></div>
            <div class="boxBody" id="mbest">
                <table class="pbox">
                    <tr>
                        <td><b>Type</b></td>
                        <td><b>Tournament</b></td>
                        <td><b>Opponent</b></td>
                        <td><b>Td</b></td>
                        <td><b>Ki</b></td>
                        <td><b>Score</b></td>
                        <td><b>Result</b></td>
                        <td><b>Match</b></td>
                    </tr>
                    <?php
		foreach (array('td' => 'scorer', 'ki' => 'killer') as $s => $desc) {
			$been_there = false;
			$matches = $p->getMatchMost($s);
			foreach ($matches as $entry) {
				if (!$been_there)
					echo "<tr><td colspan='8'><hr></td></tr>";
?>
                            <tr>
                                <?php
				$m = $entry['match_obj'];
				if ($been_there) {
					echo '<td></td>';
				}
				else {
					echo "<td><i>Top $desc: " . count($matches) . " times</i></td>";
					$been_there = true;
				}
?>
                                <td><?php echo get_alt_col('tours', 'tour_id', $m->f_tour_id, 'name'); ?></td>
                                <td><?php echo ($p->owned_by_team_id == $m->team1_id) ? $m->team2_name : $m->team1_name; ?></td>
                                <td><?php echo $entry['td']; ?></td>
                                <td><?php echo $entry['ki']; ?></td>
                                <td><?php echo $m->team1_score .' - '. $m->team2_score; ?></td>
                                <td><?php echo matchresult_icon((($m->is_draw) ? 'D' : (($m->winner == $p->owned_by_team_id) ? 'W' : 'L'))); ?></td>
                                <td><a href='javascript:void(0)' onClick="window.open('index.php?section=fixturelist&amp;match_id=<?php echo $m->match_id;?>');">[view]</a></td>
                            </tr>
                            <?php
			}
		}
?>
                </table>
            </div>
        </div>
    </div>
    <?php

?>
    <!-- Default open/close values for box -->
    <script language="JavaScript" type="text/javascript">
        document.getElementById('mbest').style.display  = 'none';
    </script>
    <?php
	}

	/**
	 * _recentGames function.
	 *
	 * @access private
	 * @return void
	 */
	private function _recentGames()
	{
		global $lng;
		$p = $this; // Copy. Used instead of $this for readability.

?>
    <div class="row">
        <div class="pboxLong">
            <div class="boxTitle3"><a href='javascript:void(0);' onClick="obj=document.getElementById('played'); if (obj.style.display != 'none'){obj.style.display='none'}else{obj.style.display='block'};"><b>[+/-]</b></a> &nbsp;<?php echo $lng->getTrn('secs/playerprofile/playedmatches');?></div>
            <div class="boxBody" id="played">
                <?php
		HTMLOUT::recentGames(STATS_PLAYER, $p->player_id, false, false, false, false, array('n' => MAX_RECENT_GAMES, 'url' => "index.php?section=coachcorner&player_id=$p->player_id"));
?>
            </div>
        </div>
    </div>
    <?php

?>
    <!-- Default open/close values for box -->
    <script language="JavaScript" type="text/javascript">
        document.getElementById('played').style.display = 'block';
    </script>
    <?php
	}

}

?>
